# Build recipe for udevil.
#
# Copyright (c) 2019-2020, 2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=udevil
version=20150504_ce9f1c5
release=5

# Define a category for the output of the package name
pkgcategory=tools

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch="
 https://dragora.mirror.garr.it/current/sources/$tarname
 rsync://rsync.dragora.org/current/sources/$tarname
"

description="
Mounts and unmounts devices without password.

udevil is a command line GNU/Linux program which mounts and unmounts
removable devices without a password, shows device info, and monitors
device changes.  It can also mount ISO files, nfs://, smb://,
ftp://, ssh:// and WebDAV URLs, and tmpfs/ramfs filesystems.
"

homepage=https://github.com/ignorantguru/udevil
license=GPLv3+

# Source documentation
docs="AUTHORS COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Update for hosts based on musl
    cp -f "${worktree}/archive/common/config.guess" config.guess
    cp -f "${worktree}/archive/common/config.sub" config.sub

    # https://github.com/IgnorantGuru/udevil/issues/89
    patch -Np1 -i "${worktree}/patches/udevil/exfat-nonempty-mountoption.patch"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --disable-systemd \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} libdir=/usr/lib${libSuffix} \
     DESTDIR="$destdir" install-strip

    # Udev rule to enable kernel polling on removable devices
    install -p -m 644 \
     "${worktree}/archive/udevil/61-removable-storage-polling.rules" -D \
     "${destdir}/etc/udev/rules.d/61-removable-storage-polling.rules"

    # To handle (dot) .new files via graft(1)
    touch "${destdir}/etc/udevil/.graft-config"

    # Restrict execution of udevil to the 'plugdev' group
    chown root:plugdev "${destdir}/usr/bin/udevil"
    chmod u+s,go-s,o-x "${destdir}/usr/bin/udevil"

    # Install devmon (udevil) perp service

    mkdir -p "${destdir}/etc/perp/devmon"

    cp -p "${worktree}/archive/udevil/rc.log" \
          "${worktree}/archive/udevil/rc.main" \
          "${destdir}/etc/perp/devmon/"

    chmod 755 "${destdir}"/etc/perp/devmon/rc.*

    # Be an active service by default
    chmod +t "${destdir}/etc/perp/devmon"

    # To handle config file(s)
    touch "${destdir}/etc/perp/devmon/.graft-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

